# Concave
# Copyright (C) 2012 Daniel Malmqvist
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import constants
import g

class MagicEffect():
    def __init__(self, magicId, target, amount = 1, modulo = 1):
        self.type = magicId
        self.target = target
        self.amount = max(1,int(amount))
        self.modulo = modulo
        self.magic = g.getMagics()[magicId]
        self.timeLeft = self.maxTime = self.magic[7]

    def update(self):
        """
        Updates this magiceffect,
        if timeLeft%modulo == 0 the effect of this magic will happen
        
        Returns true when this effect is ready to be removed
        """
        self.timeLeft -= 1
        
        if self.timeLeft % self.modulo == 0:
            if self.type == constants.HEALMAGIC():
                if self.target.hp > 0:
                    self.target.hp = min(self.target.hp + self.amount, self.target.maxHp)
            
            elif self.type == constants.FIREMAGIC():
                if self.target.hp > 0:
                    self.target.hp = max(self.target.hp - self.amount, 0) 
        
        
        if self.timeLeft > 0:
            return True
        
        return False
    
    def getImage(self):
        return g.getFire(self.timeLeft)
    